package com.media;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.baselibrary.utils.FileUtils;
import com.baselibrary.utils.StorageUtils;
import com.baselibrary.utils.ToastUtils;
import com.media.image.ImageModel;
import com.xinlan.imageeditlibrary.EditImgMainActivity;
import com.xinlan.imageeditlibrary.editimage.utils.BitmapUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.Locale;

/**
 * @author 关忆北丶
 * @description:
 * @date :2020/9/4 18:16
 */
public class AlbumManageUtils {
    //将长度转换为时间
    static StringBuilder mFormatBuilder = new StringBuilder();
    static Formatter mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());

    public static boolean isSelect(ArrayList<ImageModel> selectImageList) {
        return selectImageList != null && (selectImageList.size() == 1 || selectImageList.size() == 0);

    }

    private static final String TAG = "AlbumManageUtils";

    /**
     * @param allImgList
     * @param allVideoList
     */
    public static void notifyList(ArrayList<ImageModel> selecImageList, ArrayList<ImageModel> allImgList, ArrayList<ImageModel> allVideoList) {
        if (selecImageList == null || selecImageList.size() == 0)
            return;
        for (ImageModel imageModel : selecImageList) {
            if (imageModel.getType().equals(ImageModel.TYPE_VIDEO)) {
                if (allVideoList.contains(imageModel)) {
                    int i = allVideoList.indexOf(imageModel);
                    Log.d(TAG, "notifyList: " + i);
                    if (i != -1) {
                        allVideoList.set(i, imageModel);
                    }
                    continue;
                }
            }
            if (imageModel.getType().equals(ImageModel.TYPE_IMAGE)) {
                if (allImgList.contains(imageModel)) {
                    int i = allImgList.indexOf(imageModel);
                    Log.d(TAG, "notifyList: " + i);
                    if (i != -1) {
                        allImgList.set(i, imageModel);
                    }
                }
            }


        }

    }

    /**
     * @param allImgList
     * @return 从原集合查找选中的数据，并替换
     */
    public static ArrayList<ImageModel> notifyImgList(ArrayList<ImageModel> selectImageList, ArrayList<ImageModel> allImgList) {
        if (selectImageList == null || selectImageList.size() == 0 || allImgList == null || allImgList.size() == 0)
            return allImgList;
        ArrayList<ImageModel> imageModels = setSelectSort(selectImageList);
        for (ImageModel imageModel : imageModels) {
            if (allImgList.contains(imageModel)) {
                int i = allImgList.indexOf(imageModel);
                if (i != -1)
                    allImgList.set(i, imageModel);
            }
        }
        return allImgList;
    }

    /**
     * @param allVideoList
     * @return 从原集合查找选中的数据，并替换
     */
    public static ArrayList<ImageModel> notifyVideoList(ArrayList<ImageModel> selectImageList, ArrayList<ImageModel> allVideoList) {
        if (selectImageList == null || selectImageList.size() == 0 || allVideoList == null || allVideoList.size() == 0)
            return allVideoList;
        for (ImageModel imageModel : selectImageList) {
            if (allVideoList.contains(imageModel)) {
                int i = allVideoList.indexOf(imageModel);
                if (i != -1)
                    allVideoList.set(i, imageModel);
            }
        }
        return allVideoList;
    }

    /**
     * @return 从原集合查找选中的数据，并替换
     */
    public static ArrayList<ImageModel> notifyAllList(ArrayList<ImageModel> selectImageList, ArrayList<ImageModel> AllList) {
        if (selectImageList == null || selectImageList.size() == 0 || AllList == null || AllList.size() == 0)
            return AllList;
        for (ImageModel imageModel : selectImageList) {
            if (AllList.contains(imageModel)) {
                int i = AllList.indexOf(imageModel);
                if (i != -1)
                    AllList.set(i, imageModel);
            }
        }
        return AllList;
    }


    public static ArrayList<ImageModel> getAllImgAndVideo(ArrayList<ImageModel> allImgList, ArrayList<ImageModel> allVideoList) {
        ArrayList<ImageModel> itemImageList = new ArrayList<>();
        if (allImgList.size() > 0) {
            itemImageList.addAll(allImgList);
        }

        if (allVideoList.size() > 0) {
            itemImageList.addAll(allVideoList);
        }
        return itemImageList;
    }

    /**
     * 图片被选中
     *
     * @param imageModel
     */
    public static void selectImg(ImageModel imageModel, View relativeView, ImageView imageView, View btn) {
        if (imageModel.getType().equals(ImageModel.TYPE_VIDEO)) {
            btn.setAlpha(0.2f);
            imageView.setAlpha(0.2f);
            relativeView.setEnabled(false);
            imageView.setEnabled(false);
            // btn.setEnabled(false);
        } else {
            btn.setAlpha(1f);
            imageView.setAlpha(1f);
            relativeView.setEnabled(true);
            imageView.setEnabled(true);
            //btn.setEnabled(true);
        }
    }

    /**
     * 视频被选中
     *
     * @param imageModel
     */
    public static void selectVideo(ImageModel imageModel, View relativeView, ImageView imageView, View btn) {
        if (imageModel.getType().equals(ImageModel.TYPE_IMAGE)) {
            btn.setAlpha(0.2f);
            imageView.setAlpha(0.2f);
            relativeView.setEnabled(false);
            imageView.setEnabled(false);
            //btn.setEnabled(false);
        } else {
            btn.setAlpha(1f);
            imageView.setAlpha(1f);
            relativeView.setEnabled(true);
            imageView.setEnabled(true);
//            btn.setEnabled(true);
        }
    }

    /**
     * 选中排序
     *
     * @param selecImageList
     * @return
     */
    public static ArrayList<ImageModel> setSelectSort(ArrayList<ImageModel> selecImageList) {
        if (selecImageList == null || selecImageList.size() == 0)
            return selecImageList;
        int size = selecImageList.size();
        int number = 0;
        for (int i = 0; i < size; i++) {
            number++;
            selecImageList.get(i).setSelsctRanking(number);
        }
        return selecImageList;
    }

    public static int getSelectType(ImageModel imageModel) {
        if (imageModel == null || TextUtils.isEmpty(imageModel.getType()))
            return 0;
        if (imageModel.getType().equals(ImageModel.TYPE_IMAGE))
            return 1;
        if (imageModel.getType().equals(ImageModel.TYPE_VIDEO))
            return 2;
        return 0;
    }

    public static boolean getSelectVideoTotal(Activity activity, ArrayList<ImageModel> selecImageList, int videoMinTime) {
        int count = 0;
        for (ImageModel imageModel : selecImageList) {
            count += imageModel.duration;
            if (count > videoMinTime)
                break;
        }
        if (count <= videoMinTime) {
            ToastUtils.showShort(activity.getApplicationContext(), "视频太短啦~最少要够3秒哦~");
            return false;
        }
        return true;
    }

    public ArrayList<ImageModel> selecImageList = new ArrayList<>();//选中图片

    public static ArrayList<String> startEditorAct(Context context, ArrayList<ImageModel> selectImageList) {
        ArrayList<String> mImgPathList = new ArrayList<>();
        String appPath = StorageUtils.getAppPath(StorageUtils.TEMP_PATH);

        for (int i = 0; i < selectImageList.size(); i++) {
            String filePath = appPath + "/" + System.currentTimeMillis() + "_hankol_" + i + FileUtils.getMediaSuffixName(new File(selectImageList.get(i).path));
            BitmapUtils.copyFile(selectImageList.get(i).path, filePath);
            mImgPathList.add(filePath);
            Log.d(TAG,filePath);
        }
        return mImgPathList;
    }

    public static int getCurrentVideoTotal(ArrayList<ImageModel> selecImageList) {
        int count = 0;
        for (ImageModel imageModel : selecImageList) {
            count += imageModel.duration;
        }
        return count;
    }

    public static Drawable stringToDrawable(String path) {
        FileInputStream input = null;
        try {
            input = new FileInputStream(new File(path));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

//先转换成bitmap
        Bitmap bmp = BitmapFactory.decodeStream(input);

//再转换成drawable
        @SuppressWarnings("deprecation")
        Drawable drawable = new BitmapDrawable(bmp);
        return drawable;
    }

    //将长度转换为时间
    public static String stringForTime(int timeMs) {
        int totalSeconds = timeMs / 1000;

        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;

        mFormatBuilder.setLength(0);
        if (hours > 0) {
            return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
        } else {
            return mFormatter.format("%02d:%02d", minutes, seconds).toString();
        }
    }


    public static int getSelectMax(String type, int number) {
        if (type.equals(ImageModel.TYPE_VIDEO)) {
            return 1;
        }

        if (type.equals(ImageModel.TYPE_IMAGE)) {
            return number;
        }
        return number;
    }
}
